package com.zaofan.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Article;
import com.zaofan.service.ArticleService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

@WebServlet("/admin/article")
public class ArticleController extends BaseServlet {
	private static final long serialVersionUID = -5377620282223515871L;
	private ArticleService articleService = new ArticleService();

	protected void list2(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/admin/article/articleList.jsp").forward(request, response);
	}

	protected void to_add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			request.setAttribute("action", "add");
			request.getRequestDispatcher("/admin/article/articleAdd.jsp").forward(request, response);
		} catch (Exception e) {
		}
	}

	protected void to_edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Article article = articleService.findById(id);
			request.setAttribute("article", article);
			request.setAttribute("action", "update");
			request.getRequestDispatcher("/admin/article/articleAdd.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<Article> articles = articleService.findAll(params);
			Long count = articleService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", articles, count, response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("查询失败!!", response);
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Article article = articleService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", article, response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("查询失败!!", response);
	}

	protected void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Article article = WebUtils.write2bean(request, Article.class);
			articleService.delete(article);
			JsonUtils.writeSuccessJson("删除成功", "", response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("删除失败!!", response);
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Article article = WebUtils.write2bean(request, Article.class);
			articleService.add(article);
			JsonUtils.writeSuccessJson("添加成功!!", "", response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("属性错误!!", response);
		}
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Article article = WebUtils.write2bean(request, Article.class);
			articleService.updateArticle(article);
			JsonUtils.writeSuccessJson("更新成功!!", "", response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("更新失败!!", response);
		}
	}

}
